#include <reg51.h>
#include <intrins.h>
#define uint  unsigned int 
#define uchar unsigned char
sbit  SCL=P2^0;
sbit  SDA=P2^1; 
void Delay24(void) 
{
    unsigned char i;
    for(i=0;i<20;i++); 
}
void Delay24_10ms(void)
{
   unsigned int i;
   for(i=0;i<500;i++);
}
void start24(void)      
{ Delay24();
  SDA=0;            
  Delay24();
  SCL=0;
  Delay24();          
}
void stop24(void)
{ Delay24();
  SCL=1;
  Delay24();
  SDA=1;
  Delay24();          
}

 unsigned char ReadByte(void)
{
     unsigned char i,j;
     for(i=0;i<8;i++)     
     {
      SDA=1;
      Delay24();
      SCL=1;
      Delay24();
      j<<=1;
      j|=(bit)SDA;  
      Delay24();
      SCL=0;
      Delay24();
     }
   SDA=0;
   return(j);
}
void SendByte(unsigned char SendDat)
{
   unsigned char i,j;
   for(i=0;i<8;i++)      
    {j=SendDat;
     SDA=j&0x80;         
     Delay24();
     SCL=1;
     Delay24();
     SCL=0;
     SendDat<<=1;      
    }
   SDA=1;
   Delay24();
   SCL=1;
   Delay24();
   while(SDA==1);       
   SCL=0;
   Delay24();
   SDA=0;
   Delay24();
}

void WriteIIC(unsigned char Wcmd,add,dat)
{ 
   SDA=1;
   SCL=1;
   Delay24();
   start24();           
   SendByte(Wcmd);      
   SendByte(add);       
   SendByte(dat);      
   stop24();
   Delay24_10ms();
}

unsigned char ReadIIC(unsigned char Wcmd,add,Rcmd)
{  
   unsigned char i;
   SDA=1;
   SCL=1;
   Delay24();
   start24();           
   SendByte(Wcmd);     
   SendByte(add);       
   SDA=1;
   SCL=1;
   start24();
   SendByte(Rcmd);     
   i=ReadByte();
   stop24();  
   return(i);
}